home *** CD-ROM | disk | FTP | other *** search
/ Beyond Sanit-E 4 / Beyond Sanit-E 4 (Disk 2 of 2).adf / TXTdata / Assembler.txt / Assembler.txt
Encoding:
Text File  |  1992-09-02  |  18.6 KB  |  401 lines

  1. @5
  2.       ####            ###             ##      ########       ######
  3.      ### ##          ## ##            ##    ###  ## ###     ###   ##
  4.     ###   ##  ####   #  ##           ###   ##    ##   ##    ##    ##
  5.     ##    ## ##      #  #            ###  ##     ##    ##  ##     ##
  6.     ##    ## ##      # ##           ###   #      ##    ##  ##     ##
  7.     ##    #  ##      ###            ####  ##     ##    ##   ##
  8.     ###       #########             # ##   ### # ##     #    ######
  9.       ####       ##  ##    ##    ########   #### #     ##  ####
  10.         ####         ##    #      ##  ##         #     ## ###      ##
  11.      #     ##  #########    #      ##  ##         #    ##  ###      ##
  12.      ###  ###  ##   ######  ##   ###    ##        ######    #########  ##
  13.       ######    #####        ######      #         ###        #####    ##
  14.  
  15. *3
  16. @2
  17.                                  PRESENTS HIS
  18. *4
  19.                           Assembly Language Tutorial
  20.  
  21. *3
  22. @4
  23.                                     ENJOY!!
  24.  
  25.  
  26. @2]5INTRODUCTION
  27. *1]1@1
  28. Before  starting  this I suggest that you have a good understanding of@4 BASIC@1 in
  29. any  shape  or form.  You will also need an assembler (NO SHIT!).  A reasonable
  30. understanding of AmigaDOS is also an advantage but not essential.
  31.  
  32. I  am  very  sorry  in  advance  for  any spelling mistakes, the overuse of any
  33. particular word...  and for just living!
  34.  
  35. You  could  read the AmigaDOS tutorial that's in this very issue, kindly written
  36. by Adrian of Future PD.
  37.  
  38. Having  had  my  Amiga  for  a  few  years,  well 4 in all.  I have had lots of
  39. experience with it and although I can code.  I cannot code the latest mega demo
  40. with  the  best  "triple  whamee" 3D vector type thingy, but I do have a little
  41. experience with Assembly langauge.  This is why I have chosen to do an Assembly
  42. language  tutorial.   What  I  am hoping is that I can teach you the basics and
  43. then  someone  with  a  little more experience will write to me and offer their
  44. services  for  a  more  in-depth  tutorial.  I don't know if it is the same for
  45. everyone  but  when  I  sit  down to read a book on coding, it just seems to go
  46. right over my head, no matter how much effort I put into it.
  47.  
  48. Special  thanks  go  to  Paul  Overaa  because without his well written books I
  49. wouldn't have learnt a thing.  Cheers Paul!
  50. *4@2
  51.                     INTRODUCTION INTO PROGRAMMING THE 680X0
  52. *1@1
  53. In  all  the  books that I have read, all coding is done the "system legal" way
  54. which  means  to say that if you want to learn to program the Amiga the correct
  55. way,  then  you  really  need  to  get  hold of the Amiga Developer's Kit which
  56. contains  all  the  Commodore  include  files needed to "talk" to the operating
  57. system.   Essential reading for the would-be coder would include the ROM Kernal
  58. Manuals, but at £30 quid they aren't cheap.  As most of the people reading this
  59. article  will hopefully want to get somewhere by reading this tutorial.  I mean
  60. most people will want to learn so they can code a little intro for a production
  61. or  a little hack that you've always needed.  I will not be teaching you how to
  62. become  a  master coder because, it's not that I don`t want to, I really wish I
  63. could, but I don't have a big enough understanding myself yet.
  64.  
  65. I  must  first  ask that if you are really intent on learning Assembler then it
  66. has  to  be worth investing some money in it.  I have recommended some books in
  67. this  tutorial  and  where  they can be obtained.  If you are like me and don't
  68. have  much  of  an  income then I must recommend the series called HOW TO CODE.
  69. Although  this  is  now  no  longer  supported  it  is still about 450K of text
  70. relating  to  coding, covering topics including Reading_C, Vectors, Coppers and
  71. many more.  It is only the cost of a disk as well.  If should be available from
  72. any self respecting Public Domain library.
  73. @2
  74. HERE GOES!!
  75. @1]5
  76. First things first -
  77. ]1
  78. In this series of tutorials I intend to deal with the following topics:
  79. *3@4
  80.                              CO-PROCESSOR HARDWARE
  81.                            BITPLANES AND PLAYFIELDS
  82.                                 SPRITE HARDWARE
  83.                                 AUDIO HARDWARE
  84. *1@1
  85. These parts are covered in depth in the Hardware manual and are given a chapter
  86. each.   This  book  has  to  be  recommended  to all future demo coders.  As it
  87. provides  essentials  on the use of "hardware bashing" or coding the non-system
  88. way.
  89.  
  90. These  will  be  the  main parts.  I may include such things as Blitter, system
  91. control  and  interfacing in later issues if the tutorial is going well enough.
  92. Included  in with all this will be the topics of DMA (Direct Memory Access) and
  93. smaller  topics  such  as  the  various  addressing  modes  and  all  the other
  94. bollocks!!   In  this  chapter  we  begin with opening libraries and the use of
  95. copperlists.
  96. @2]5
  97. The Basics first -
  98. @1]1
  99. Computers  have a logical way of accepting data, this data is called Binary and
  100. consists of loads of 1's and 0's.  The computer reads these and interprets them
  101. as 1(on) or 0(off).
  102.  
  103. It  is these Binary combinations that are used to make other numbers.  E.G.  If
  104. we  use  just  one  bit there are two combinations 1 and 0.  If we use two bits
  105. there  are  4 combinations; 00, 01, 10, 11.  Carrying this on to 8-bit gives us
  106. 256 possible commbinations and therefore 256 possible numbers.
  107.  
  108. We normally count from 0-9.  This is known as Base 10 and you don't really need
  109. this for Assembler.
  110.  
  111.                          1 byte  - 8 bits - a BYTE .b
  112.                          2 bytes - 16bits - a WORD .w
  113.                          4 bytes - 32bits - a LONG WORD .l
  114.  
  115. Although  using  Binary is all well and good for your computer it does become a
  116. bit  confusing  when  trying  to  decode  a string of 1 and 0.  This is why, in
  117. Assembly, we count in Base 16.  That is to say we go from 1 to F.
  118.  
  119.                          1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  120.  
  121. So  10  in  Hexadecimal is really 16 in normal Base 10.  For numbers over 15 we
  122. use two digits.
  123.  
  124. Therefore,  work out the following in normal Base 10 numbers as a little puzzle
  125. to keep you awake!
  126.  
  127.                               $0F,$10,$20,$FE,$FF
  128.  
  129. You  should  have got 15,16,32,255,256 in that order the $ sign is used to tell
  130. the Assembler that the number is HEXADEMICAL.
  131.  
  132. Doing even more we get:
  133.  
  134.                                   255 = $00FF
  135.                                   256 = $0100
  136.                                    etc......
  137.                                  4096 = $1000
  138.                                  65535 = $FFFF
  139.  
  140. You  should recognise some of the numbers.  Remember the non-AGA machines, well
  141. they could only have 4096 colours!  It's all to do with numbers!
  142.  
  143. This  is  the  hardest  thing  to understand when it comes to learning Assembly
  144. language.
  145. *4@2
  146. Let The Show Begin!
  147. *1@2
  148. Opening Libraries -
  149. @1
  150. Okay,  in  this  issue  I will deal with the fundamentals, and there is nothing
  151. more  fundamental  than  the  use  of the Exec run-time libraries.  At the very
  152. basic  level  an Assembly language program just puts a list of library calls in
  153. order  and  these  will  eventually  provide some sort of visible output.  When
  154. coding in C, using function libraries is a damn sight easier because all that's
  155. needed  is  an  open  library  call and then all functions are called using the
  156. function name together with the required parameters.
  157.  
  158. When coding in Assembler these registers have to be preloaded with any required
  159. parameters before the subroutine call, this is all transparent in C.
  160.  
  161. Okay,  so  how  do we open these libraries then?  Well, we use an Openlibrary()
  162. (described  later)  call.   This  isn`t  actually  needed  for the Exec library
  163. because it is permanently available.
  164.  
  165. The  calls  are  performed  by loading the base address of the library into the
  166. register  a6.  Then we preform an indirect subroutine call using a displacement
  167. Value  Offset  (LVO).   The  actual  code  to load the base address of the Exec
  168. library into a6 and open the lib looks like this:
  169.  
  170.                          move.l  _AbsExecBase,a6
  171.                          jsr     _LVOOpenLibrary,(a6)
  172.  
  173. Okay, so this sounds a bit complicated but just imagine the name of the library
  174. is  loaded  in  a6 and the next line just opens it.  If we imagine the, already
  175. opened,  Exec  library as a ladder in memory, the a6 register is loaded in with
  176. the start (the last rung) of the ladder, then the indirect subrountine tells us
  177. to  go  down the ladder until we reach the required function.  In this case the
  178. _LVOOpenLibrary (-552) call.
  179.  
  180. These  text  means  are resolved in two different ways.  Firstly, by the use of
  181. XREF  statements,  which  tell the Assembler to resolve them at link time.  The
  182. second  and  easier  way is to include a header file of the LVO definitions and
  183. this  is  where the include files come in.  That's all they are, just a list of
  184. the LVO functions with the corresponding EQU numbers.  This way is a lot easier
  185. as we don`t have to link with Amiga.lib library to run our program.
  186.  
  187. Okay,  so  if  you  are  a  fast  thinker then you may have noticed a potential
  188. problem.  Well, here it is:
  189.  
  190. What  if  the register a6 already contains some data that we want to use and we
  191. need  to open a library.  Well, we now come onto the subject of Macros.  Macros
  192. are  like  large  LET  BASIC  statements,  and  the  LINKLAB  one that's in the
  193. exec/libraries include file, is just what we need.  Here is, basically, what it
  194. does:
  195.  
  196.    move.l  a6,(sp)               ; Move a6 to the stack pointer to preserve it.
  197.    move.l  <LibraryPointer>,a6   ; Get the base mem address of the lib
  198.                                    into a6.
  199.    jsr     (_LVORountineName),a6 ; Make the indirect subrountine call.
  200.    move.l  (sp),a6               ; Restore a6.
  201.  
  202. So  with  the  Exec.library include in the start of the code, we can use simple
  203. library code like this:
  204.  
  205.                      LINKLAB _LVOOpenLibrary,_AbsExecBase
  206.  
  207. This  makes a basis for us to produce well written code as using Macros reduces
  208. the possibility of errors.
  209.  
  210. The  actual  code  included on this disk can be loaded into a Word Processor if
  211. you  don't  have  an  Assembler,  so  you  can  see what going on.  I have also
  212. included two executable versions of the example sources.
  213.  
  214. @5
  215. The Copper Co-processor:
  216. @1
  217. The Copper is a graphics co-processor that is inside one of the Amiga's customs
  218. chips.   It  gets  it's instructions via Direct Memory Access (DMA) or in other
  219. words  the data doesn't have to go through the processor to be executed, making
  220. it  a  lot faster.  The Copper can control the entire graphics system by itself
  221. leaving the processor to deal with more intense things.
  222.  
  223. A copperlist has three possible instructions.  These are:
  224.  
  225.                                      MOVE
  226.                                      WAIT
  227.                                      SKIP
  228. @4
  229. MOVE-@1 Move data into special purpose register.
  230. @4
  231. SKIP-@1  Skip  the  next  instruction  if  the  video  beam has reached a certain
  232. position.
  233. @4
  234. WAIT-@1 Wait for screen position.
  235.  
  236. *4@2
  237. OKAY, LET'S GO!!
  238. *1@1
  239. Load  the  example  source  that  is on this disk and move to the bottom of the
  240. screen.   The  code  is  heavily  commented so to provide a tutorial of what is
  241. going on.  The section at the bottom reads:
  242. @5
  243.                           dc.w $3401,$FFFF.$180.$F00
  244.                           dc.w $8001,$FFFF,$180.$FF0
  245.                           dc.w $8101,$FFFF,$180.$00F
  246.                           dc.w $A001,$FFFF,$180,$0F0
  247. @1
  248. This  data  is our copperlist instructions.  The first section is the positions
  249. of  our  copperlist,  try  changing  a  few of the values to get some different
  250. positions.  The next two sections will be left out for this tutorial.  The last
  251. section  deals  with the screen colours of the copperlist.  Changing these will
  252. start  the  copper with different colours on the screen.  This technique can be
  253. used  to  produce  different effects such as a german flag or a landscape (blue
  254. for the sky and green for the land).
  255.  
  256. Right,  that's  the good stuff out of the way.  We will now look at each of the
  257. instructions in our little block of code.
  258.  
  259. The  first  four  lines are equates.  They are like LET statements in BASIC and
  260. just  tell the Assembler that for every FORBID that it comes across, replace it
  261. with -132.
  262.  
  263. The  first  instruction  just tells the Assembler that this section is for prog
  264. code.  This is used again in the section cp,data_c which tells the Assembler to
  265. put  the  data  into  Chip  RAM.  If you only have Chip RAM you don't need this
  266. instruction.
  267.  
  268. The  next  instruction  is  the  Load Effective Address (lea).  This allows the
  269. Assembler  to  load  the address location into a register.  In the listing, the
  270. graphics lib address is loaded into a1.
  271.  
  272. The  next instruction is the most used of the Assembler instructions.  It moves
  273. a 16-bit word value of 4 into the a6 register.
  274.  
  275. The  next  instruction, the Jump to SubRoutine (jsr), transfers program control
  276. over  to a subroutine much like GOSUB in BASIC.  The -408 is a LVO offset which
  277. stands for the OldOpenlib function.
  278.  
  279. The  next  instruction  is now simple and you should be able to tell me what is
  280. does already.  Well, it moves a long word (32-bit) from d0,a1.  Simple, eh!
  281.  
  282. The  next  instruction  copies  the contents of the long word, whose address is
  283. made by adding 38 to the address in a1, into the defined OLDCOP string.
  284.  
  285. The  next instruction is the closelib LVOoffset and is used to close the opened
  286. graphics  library.   The  next  instruction  is  the same but it is another the
  287. Forbid call.
  288.  
  289. The  next  instruction,  if  you  didn't  know.   moves the defined #NC, into a
  290. special copper address at $dff080.
  291.  
  292. The  next  instruction,  the btst, stands for "test a bit", and it will test an
  293. operand bit and set the zero flag.  In this case it is used to test for a mouse
  294. button  press.  It waits until the address at BFE001 is equal to 6.  When it is
  295. the  program  contines.   If  the  left  button is pressed, the value in BFE001
  296. changes to 6, therefore stopping the program.
  297.  
  298. The next instruction, the bne, stands for "branch if not equal".  The branch is
  299. taken  if  the  zero  bit  is  0.  This is the opposite to beq which stands for
  300. "branch if equal".  It will continue to loop until the branch is equal.
  301.  
  302. The next instruction is another LVO function known as Permit.
  303.  
  304. The  next  instruction  should  not  cause  any  problems now I hope.  It moves
  305. 32-bits from the address at OLDCOP into memory location $dff080.
  306.  
  307. The  next  instrution is a new one.  It stands for "move quick".  It is used to
  308. set  a  value  very  quickly.   Most  Assembler's  will  optimise any immediate
  309. addressing moves (i.e.  normal moves to moveq's).  Thus increasing code speed.
  310.  
  311. The next instruction is a Return To Subroutine (rts).  This instruction returns
  312. control back to a address given by the stack.
  313.  
  314. The   next   instruction,   a   define   instruction  graplib,  is  defined  as
  315. "graphics.library".  It also reserves space in memory.
  316.  
  317. OLDCOP defined as 0 - constant.
  318.  
  319. NC:   This  is our new copperlist data.  The instruction above this tells us to
  320. put it in Chip mem.
  321.  
  322. The  lines  that are left is the code that does all the visible stuff.  This is
  323. the  main point of this little tutorial.  I hope this is a help and if you want
  324. to write to me then my address is in the sourcecode.
  325.  
  326. The last instruction, the line that reads:
  327.  
  328.                              DC.w $FFFF,FFFE ; end
  329.  
  330. ...is  an impossible instruction, and as the computer can't do it, it will stay
  331. with the copper screen until the mouse button is pressed.
  332.  
  333. As  I can only just code myself I have used some source that was included in an
  334. old  issue  of Grapevine.  I have updated this code and added a lot of comments
  335. to  it.   This  is,  I  believe, enough to justify a release.  Writing this has
  336. helped me to learn the depths of Assembly language programming.
  337.  
  338. The full copyright of the source remains with the orginal author.
  339.  
  340. If  you  want  to carry on, then you could change the mouse test bit so that it
  341. tested for a joystick press or the right mouse button or something else.
  342. *4@7
  343. Cheers, and have fun!
  344. *1@1
  345. Next  issue  sees  more  info  on  the  use  of  DMA and we will start off with
  346. bitplanes,  or  maybe  sound, or maybe sprites or interface hardware.  Or I may
  347. even  do  some intution coding, but I do find that a bit boring and always make
  348. mistakes.  You'll just have to wait and see.
  349.  
  350. AND REMEMBER     ====================>
  351.  
  352. #0            _#      **MMp     g###    `N##     _agN##P0N#           _#
  353.            g##       jN##    j##F     J##    _dN0"      "          g##
  354.          _#]##      _0 ##L  jN##F     ###  g#/#/                 _03##
  355.         gE_j##      #  0## jF ##F    j##F  j##     ______       gE_j##
  356.       _0"""N##     d"  J##L0  ##F    0##   0##     "9##F"     _0"""5##
  357.     _gF    ]##    jF    ###   ##F    ##F   `##k     d##     _gF    j##
  358.   _g#_    _j##L__g#__   ]N  _j##L_ _d##L_   `#Nh___g#N'   _g#_    _j##
  359.  `""""    """""'""""'    "  """""" """"""      """""""   `""""    """""
  360.        /\     _ // AMIGA, Amiga Technologies International.
  361.       /..\    \X/  Powered by Motorola. Intel Outside!!.
  362.      / .. \             __________________________________________________
  363.     /_...._\           |                                               __ |
  364.      |....|            |___  ___________  ___________    AMIGA 4000/40 == |
  365.      |....|            ||_|  |_|_|_|_|_|  |_|_|_|_|_|                     |
  366.      |....|            |________________________________ _______ _________|
  367.      |....|            |[_______________________________ |__|__| [_I_I_I_]|
  368.      |....|            |[___I_I_I_I_I_I_I_I_I_I_I_I_]  |    _    [_I_I_I_]|
  369.      |....| Only The   |[__I_I_I_I_I_I_I_I_I_I_I_I_L___|  _[_]_  [_I_I_I_]|
  370.      |..<-------       |[_____I_I_I_I_I_I_I_I_I_I_I____] [_I_I_] [_I_I_T ||
  371.      |....|  Makes     | [__I__I_________________I__L_] ________ [___I_I_j|
  372.      |....|   It       |                                                  |
  373.      |....| Possible   |__________________________________________________|
  374.  
  375. @2Refence Material. 
  376.  
  377. Function Name: OpenLibrary()
  378. Description: Opens a Amiga Run-Time library.
  379. Call Format: Base_address=Openlibrary(library_name,version);
  380. Registers:    D0    A1    D0
  381. Arguments: Library_name.  Address of a null terminated string version -
  382. a library version number.
  383. Return Value: Base_address - the address of the base of the library.  If the
  384. library cannot be opened a NULL (0) value is returned.
  385.  
  386. Function Name: CloseLibrary()
  387. Description: Close an open library.
  388. Call format: CloseLibrary(base_address);
  389. Registers: A1
  390. Arguments: base_address - the library base address.
  391. Return Value: None.
  392.  
  393. Function Name: Write()
  394. Description: Write data to a file.
  395. Call Format: length_written = Write(file,buffer_p,data_length)
  396. Registers:       D0             D1       D2      D3
  397. Arguments: file - file handle
  398.            buffer_p - pointer to buffer holding the data
  399.            data_length - length of the data
  400. Return Value: length_written - number of bytes actually written.
  401. Notes: A length_written value of -1 will indicate and error.